﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy5_10
{
    public partial class Form1 : Form
    {
        private const double PI = 3.14159;
        public Form1()
        {
            InitializeComponent();
            this.Text = "多瓣花"; // 设置窗体标题
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                // 1. 获取输入的样式数(d)和花瓣数(k)
                int d = int.Parse(txtStyle.Text);
                int k = int.Parse(txtPetal.Text);

                // 2. 清空PictureBox之前的绘制
                pictureBox1.Refresh();
                Graphics g = pictureBox1.CreateGraphics();
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; // 抗锯齿

                // 3. 平移坐标系到PictureBox中心
                int centerX = pictureBox1.ClientSize.Width / 2;
                int centerY = pictureBox1.ClientSize.Height / 2;
                g.TranslateTransform(centerX, centerY);

                // 4. 初始化随机数（生成彩色线条）
                Random ran = new Random();
                Pen p = new Pen(Color.Black);

                // 5. 计算Δα = π/d（样式数对应的角度增量）
                double deltaA = PI / d;

                // 6. 循环绘制：α从0到2π，步长π/180（每次放大1.5度）
                for (double a = 0; a <= 2 * PI; a += PI / 180)
                {
                    // 生成随机颜色
                    p.Color = Color.FromArgb(ran.Next(256), ran.Next(256), ran.Next(256));

                    // 计算点1(x1,y1)：α代入参数方程
                    double x1 = 50 * (1 + Math.Sin(k * a)) * Math.Cos(a);
                    double y1 = 50 * (1 + Math.Sin(k * a)) * Math.Sin(a);

                    // 计算点2(x2,y2)：α+Δα代入参数方程
                    double a2 = a + deltaA;
                    double x2 = 50 * (1 + Math.Sin(k * a2)) * Math.Cos(a2);
                    double y2 = 50 * (1 + Math.Sin(k * a2)) * Math.Sin(a2);

                    // 绘制两点之间的线段（构成立体花瓣）
                    g.DrawLine(p, (float)x1, (float)y1, (float)x2, (float)y2);
                }

                // 7. 释放资源
                p.Dispose();
                g.Dispose();
            }
            catch (Exception ex)
            {
                // 捕获输入错误（如非数字）
                MessageBox.Show($"输入错误：{ex.Message}", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
